package com.erpanderp.wam.ac.model.service.travel;

import java.util.List;
import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;
import com.erpanderp.wam.ac.model.entity.travel.TravelLogistics;
import com.erpanderp.wam.util.HibernateUtil;

public class TravelLogisticsDAO {

	public List<TravelLogistics> getTravelLogistics(Long travelRequestId)
	{
		List<TravelLogistics> travelLogistics = null;
		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from TravelLogistics tl where tl.travelRequest.travelRequestId = :travelRequestId";
			Query query = session.createQuery(queryString);
			query.setCacheable(false).setParameter("travelRequestId", travelRequestId);
			travelLogistics = (List<TravelLogistics>) query.list();
			session.getTransaction().commit();
			return travelLogistics;
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	
	public boolean saveTravelLogistics(List< TravelLogistics> travelLogistics)
	{
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			for(TravelLogistics logistics : travelLogistics){
				session.save(logistics);
			}
			session.getTransaction().commit();
			return true;
			
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}		
	}
	
}
